#include "QGrapheScene.h"

QGrapheScene::QGrapheScene(QWidget *parent):QGraphicsScene(parent)
{

    graphe = new Biparti();
    graphe->generer_graphe_complet(6);
    graphe->initialisationCouleur(0);

    this->dessiner();

}
QGrapheScene::~QGrapheScene(){}


Graphe* QGrapheScene::getGraphe(){return graphe;}
void QGrapheScene::setGraphe(Biparti *g){
    clearGraphe();
    graphe = g;
}


void QGrapheScene::clearGraphe(){
    QList<QGraphicsItem *> itemList = this->items();
    int sizeList = itemList.size();
    for (int i=0;i<sizeList;i++){
        this->removeItem(itemList[i]);
    }
}

void QGrapheScene::dessinerSommets(Graphe * graphe){
    for (int i=0;i<graphe->getN();i++)
    {

        Sommet *p = graphe->getSommets()[i];



        QGraphicsEllipseItem *circle = new QGraphicsEllipseItem(p->getX(),p->getY(),20,20);
        circle->setBrush(Qt::blue);

        this->addItem(circle);
    }
}
void QGrapheScene::dessinerAretes(Graphe * g){
    QPen pen;
    pen.setStyle(Qt::SolidLine);
    pen.setWidth(3);
    pen.setBrush(Qt::black);
    vector<Arete*>  aretes = g->getAretes();


    for (int i=0;i<g->getM();i++)
    {

        Arete *a = aretes.at(i);
        setBrush();
        QGraphicsLineItem * line = new QGraphicsLineItem(a->getU()->getX()+20,a->getU()->getY()+10,a->getV()->getX(),a->getV()->getY()+10);
        line->setPen(pen);
        addItem(line);


    }
}
void QGrapheScene::setBrush(QPen* pen,int couleur){


    switch (couleur){
        case 0: pen->setBrush(Qt::black);break;
        case 1: pen->setBrush(Qt::yellow);break;
        case 2: pen->setBrush(Qt::blue);break;
        case 3: pen->setBrush(Qt::red);break;
        case 4: pen->setBrush(Qt::green);break;
        case 5: pen->setBrush(Qt::magenta);break;
        case 6: pen->setBrush(Qt::cyan);break;
        case 7: pen->setBrush(Qt::darkBlue);break;
        case 8: pen->setBrush(Qt::darkGray);break;
        case 9: pen->setBrush(Qt::darkRed);break;
        case 10: pen->setBrush(Qt::darkMagenta);break;
        default : pen->setBrush(Qt::darkYellow);
        }


                    }
    void QGrapheScene::dessiner(){

        this->dessinerAretes(graphe);
        this->dessinerSommets(graphe);

    }
    void QGrapheScene::dessiner_sous_graphe(int c1,int c2){

      sous_graphe =  graphe->sous_graphe(c1,c2);
      this->dessinerAretes(sous_graphe);
      this->dessinerSommets(sous_graphe);

    }
    void QGrapheScene::nouveau(int nb, int delt,int mode){
        clearGraphe();
        graphe = new Biparti();
        if (mode) graphe->generer_graphe_complet(nb);
        else graphe->generer2(nb,delt);

        graphe->initialisationCouleur(0);
        dessiner();


    }
